package cn.schoolwow.workflow.test.module.oa.common.flow.common;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployNode;
import cn.schoolwow.workflow.domain.task.WorkFlowAssigner;
import cn.schoolwow.workflow.domain.task.WorkFlowAssignerType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ConvertDeployAssignerTypeAndAddAssignerCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONArray array = flowContext.getData("array", JSONArray.class);
        if(null==array||array.isEmpty()){
            return;
        }
        WorkFlowDeployNode workFlowDeployNode = flowContext.checkInstanceData(WorkFlowDeployNode.class);

        List<WorkFlowAssigner> workFlowAssignerList = new ArrayList<>();
        for(int i=0;i<array.size();i++){
            JSONObject o = array.getJSONObject(i);
            WorkFlowAssigner workFlowAssigner = convertAssigneeType(o, flowContext);
            workFlowAssignerList.add(workFlowAssigner);
        }
        workFlowDeployNode.assigner = JSON.parseArray(JSON.toJSONString(workFlowAssignerList));
    }

    @Override
    public String name() {
        return "转换审批人类型";
    }

    private WorkFlowAssigner convertAssigneeType(JSONObject o, FlowContext flowContext){
        String fieldName = flowContext.checkData("fieldName", String.class);

        WorkFlowAssigner workFlowAssigner = new WorkFlowAssigner();
        workFlowAssigner.assigneeType = WorkFlowAssignerType.SpecificAssigner.value;
        int assigneeType = o.getIntValue(fieldName);
        switch (assigneeType){
            case 0:{
                workFlowAssigner.assignerList.add("{{内置变量.发起人}}");
            }break;
            case 1:{
                throw new IllegalArgumentException("审批人暂不支持[上级]");
            }
            case 2:{
                throw new IllegalArgumentException("审批人暂不支持[部门负责人]");
            }
            case 3:{
                //角色
                List<Long> assigneesList = o.getJSONArray("assignees").toJavaList(Long.class);
                for(Long singleAssigner:assigneesList){
                    workFlowAssigner.assignerList.add(1+":"+singleAssigner);
                }
            }break;
            case 4:{
                //用户
                List<Long> assigneesList = o.getJSONArray("assignees").toJavaList(Long.class);
                for(Long singleAssigner:assigneesList){
                    workFlowAssigner.assignerList.add(0+":"+singleAssigner);
                }
            }break;
            case 5:{
                throw new IllegalArgumentException("审批人暂不支持[连续多级上级]");
            }
            case 6:{
                throw new IllegalArgumentException("审批人暂不支持[连续多级部门负责人]");
            }
            case 7:{
                workFlowAssigner.assigneeType = WorkFlowAssignerType.ManualAssigner.value;
            }
            default:{
                throw new IllegalArgumentException("不支持的审批人类型!值:"+assigneeType);
            }
        }
        return workFlowAssigner;
    }
}
